package com.example.mhandler;

public class MyLooper {
    static final ThreadLocal<MyLooper> sThreadLocal = new ThreadLocal<MyLooper>();
    public MyMessageQueue mQueue;

    public MyLooper() {
        mQueue = new MyMessageQueue();
    }

    public static void prepare() {
        if (sThreadLocal.get() != null) {
            throw new RuntimeException("Only one Looper may be created per thread");
        }
        sThreadLocal.set(new MyLooper());
    }

    public static void loop() {
        MyLooper myLooper=getLooper();
        for (;;){
            MyMessageQueue queue= myLooper.mQueue;
            MyMessage message= queue.next();
            if (message==null){
                return;
            }
            message.target.handlerMessage(message);
        }
    }

    private static MyLooper getLooper() {
        return sThreadLocal.get();
    }

    public static MyLooper myLooper() {
        return sThreadLocal.get();
    }
}